package com.swy.shopgroup.view;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.google.gson.Gson;
import com.sl.utakephoto.compress.CompressConfig;
import com.sl.utakephoto.exception.TakeException;
import com.sl.utakephoto.manager.ITakePhotoResult;
import com.sl.utakephoto.manager.UTakePhoto;
import com.swy.shopgroup.R;
import com.swy.shopgroup.Utils;
import com.swy.shopgroup.adapter.PageOutAdapter;
import com.swy.shopgroup.entity.Acts;
import com.swy.shopgroup.entity.Goods;
import com.swy.shopgroup.entity.OutPage;
import com.swy.shopgroup.entity.ResultBean;
import com.swy.shopgroup.entity.ShopInfo;
import com.swy.shopgroup.entity.ShopUser;
import com.swy.shopgroup.network.OkHttp;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Response;

public class PagesMeFragment extends Fragment {

    private Button commit;
    private ListView listView;
    private SwipeRefreshLayout swipe;
    private PageOutAdapter mAdapter;
    private List<OutPage.DataBean> list = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_pages, container, false);
        initView(v);
        initData();
        return v;
    }

    public void initView(View v) {
        commit = v.findViewById(R.id.commit);
        listView = v.findViewById(R.id.listView);
        swipe = v.findViewById(R.id.swipe);
        mAdapter = new PageOutAdapter(getContext(), list);
        listView.setAdapter(mAdapter);
    }

    public void initData() {
        selectPage();
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                selectPage();
            }
        });
        commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alterDiaglog = new AlertDialog.Builder(getContext(), R.style.MyDialog);
                View vv = LayoutInflater.from(getContext()).inflate(R.layout.alert_out_page, null, false);
                alterDiaglog.setView(vv);
                final AlertDialog dialog = alterDiaglog.create();
                Button commitBtn = vv.findViewById(R.id.commitBtn);
                final EditText info = vv.findViewById(R.id.info);
                final EditText name = vv.findViewById(R.id.name);
                commitBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String n = name.getText().toString();
                        if (TextUtils.isEmpty(n)) {
                            Toast.makeText(getContext(), "请输入名称", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                            return;
                        }
                        String c = info.getText().toString();
                        if (TextUtils.isEmpty(c)) {
                            Toast.makeText(getContext(), "请输入内容", Toast.LENGTH_SHORT).show();
                            dialog.dismiss();
                            return;
                        }
                        String json = Utils.getAllInfo(getContext(), Utils.shopInfo);
                        Gson gson = new Gson();
                        ShopInfo shopInfo = gson.fromJson(json, ShopInfo.class);
                        OutPage.DataBean bean = new OutPage.DataBean();
                        bean.setName(n);
                        bean.setContent(c);
                        bean.setShopId(shopInfo.getData().getId());
                        insertPages(bean);
                        dialog.dismiss();
                    }
                });
                dialog.show();
            }
        });
    }

    @SuppressLint("CheckResult")
    public void selectPage() {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                OkHttp o = new OkHttp();
                try {
                    String a = Utils.getAllInfo(getContext(), Utils.shopInfo);
                    Gson gson = new Gson();
                    ShopInfo shopInfo = gson.fromJson(a, ShopInfo.class);
                    if (shopInfo == null) {
                        return;
                    }
                    Response response = o.selectOutPage(shopInfo.getData().getId() + "");
                    e.onNext(response.body().string());
                } catch (IOException a) {
                    a.printStackTrace();
                    e.onNext("s");
                }
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new io.reactivex.functions.Consumer<String>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull String s) throws Exception {
                try {
                    Gson gson = new Gson();
                    ResultBean result = gson.fromJson(s, ResultBean.class);
                    if (result.getCode().equals("200")) {
                        list.clear();
                        OutPage outPage = gson.fromJson(s, OutPage.class);
                        list.addAll(outPage.getData());
                        mAdapter.notifyDataSetChanged();
                    }
                    Toast.makeText(getContext(), result.getMsg(), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                }
                swipe.setRefreshing(false);
            }
        });
    }

    @SuppressLint("CheckResult")
    public void insertPages(final OutPage.DataBean goods) {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                OkHttp o = new OkHttp();
                try {
                    Response response = o.insertOutPage(goods);
                    e.onNext(response.body().string());
                } catch (IOException a) {
                    a.printStackTrace();
                    e.onNext("s");
                }
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new io.reactivex.functions.Consumer<String>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull String s) throws Exception {
                try {
                    Gson gson = new Gson();
                    ResultBean result = gson.fromJson(s, ResultBean.class);
                    if (result.getCode().equals("200")) {

                    }
                    Toast.makeText(getContext(), result.getMsg(), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                }
                swipe.setRefreshing(false);
            }
        });
    }
}
